package com.yao.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.webkit.*;
import android.widget.LinearLayout;
import com.base.activity.BaseWebViewActivity;
import com.yao.Js.JsInterface;
import com.yao.R;

public class YaoWebViewActivity extends BaseWebViewActivity {

    private LinearLayout errorView;
    private boolean isERROR = false;

    public static void startActivity(Context context, String title, String url) {
        Intent intent = new Intent(context, YaoWebViewActivity.class);
        intent.putExtra(EXTRA_NAME_TITLE, title);
        intent.putExtra(EXTRA_NAME_URL, url);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    protected void initView(Bundle bundle) {
        getDecorView().setBackgroundColor(Color.parseColor("#03A9F4"));

        mWebView = findViewById(R.id.wv);
        mProgressBar = findViewById(R.id.pb);
        errorView = findViewById(R.id.error);

        errorView.setVisibility(View.INVISIBLE);
        errorView.setClickable(false);
        mWebView.setVisibility(View.VISIBLE);

        errorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isERROR = false;
                mWebView.reload();
            }
        });

        init(getIntent());

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setTextZoom(100);

        mWebView.addJavascriptInterface(new JsInterface(), "android");

        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (!isERROR) {
                    errorView.setVisibility(View.INVISIBLE);
                    errorView.setClickable(false);
                    mWebView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                int errorCode = 0;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                    errorCode = error.getErrorCode();
                }
                // 断网或者网络连接超时
                if (errorCode == ERROR_HOST_LOOKUP || errorCode == ERROR_CONNECT || errorCode == ERROR_TIMEOUT) {
                    errorView.setVisibility(View.VISIBLE);
                    errorView.setClickable(true);
                    mWebView.setVisibility(View.INVISIBLE);
                    isERROR = true;
                }
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                // 断网或者网络连接超时
                if (errorCode == ERROR_HOST_LOOKUP || errorCode == ERROR_CONNECT || errorCode == ERROR_TIMEOUT) {
                    errorView.setVisibility(View.VISIBLE);
                    errorView.setClickable(true);
                    mWebView.setVisibility(View.INVISIBLE);
                    isERROR = true;
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void release() {

    }
}
